package net.alvin;

import net.alvin.gitlab.GitlabStatistics;
import org.apache.commons.cli.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.FileNotFoundException;

public class GitlabHelper {

    private static final Logger logger = LogManager.getLogger(GitlabHelper.class);

    public static final String DEFAULT_OUTPUT_PATH = "D:\\gitlab-projects.xlsx";
    public static final long DEFAULT_BIG_PROJECT_SIZE = 1073741824L;

    /**
     * 生成命令行参数清单
     *
     * @return
     */
    private static Options generateOptions() {
        final Options options = new Options();
        options.addOption(new Option("h", "help", false, "获取命令行参数帮助信息"));
        options.addOption(new Option("u", "url", true, "[必要参数] Gitlab 服务器URL地址，格式示例：http://192.168.0.253/gitlab/"));
        options.addOption(new Option("t", "token", true, "[必要参数] Gitlab 服务器访问Token"));
        options.addOption(new Option("f", "file", true, "[非必要参数] 下载文件本地保存路径，默认值：" + DEFAULT_OUTPUT_PATH));
        options.addOption(new Option("s", "size", true, "[非必要参数] 大工程存储空间大小，默认值：" + DEFAULT_BIG_PROJECT_SIZE));
        return options;
    }

    /**
     * 解析命令行入参
     *
     * @param options
     * @param args
     * @return
     */
    private static CommandLine parseCommandLine(final Options options, final String[] args) {

        final CommandLineParser cmdLineParser = new DefaultParser();
        CommandLine commandLine = null;
        try {
            commandLine = cmdLineParser.parse(options, args);
            if (commandLine.hasOption('h')) {
                // 打印使用帮助
                HelpFormatter hf = new HelpFormatter();
                hf.setWidth(160);
                hf.printHelp("GitlabHelper", options, true);
                System.exit(0);
            } else if (!commandLine.hasOption('u') ||
                    !commandLine.hasOption('t')) {
                // 打印使用帮助
                HelpFormatter hf = new HelpFormatter();
                hf.setWidth(160);
                hf.printHelp("Gitlab仓库助手(GitlabHelper)", options, true);
                System.exit(0);
            }
        } catch (ParseException parseException) {
            logger.error("ERROR: 命令行入参 {} 解析错误， 错误原因：{}", args, parseException);
        }
        return commandLine;
    }

    public static void main(String[] args) throws Exception {
        final CommandLine cmdLine = parseCommandLine(generateOptions(), args);
        final String gitlabUrl = cmdLine.getOptionValue('u');
        final String token = cmdLine.getOptionValue('t');
        final String file = cmdLine.hasOption('f') ? cmdLine.getOptionValue('f') : DEFAULT_OUTPUT_PATH;
        final long size = cmdLine.hasOption('s') ? Long.parseLong(cmdLine.getOptionValue('s')) : DEFAULT_BIG_PROJECT_SIZE;
        try {
            GitlabStatistics statistics = new GitlabStatistics(gitlabUrl, token, file);
            statistics.writeProjectSheet(size);
            statistics.writeUserSheet();
            statistics.writeAndCloseExcel();
        }catch (FileNotFoundException e){
            logger.error("ERROR: 文件 {} 未找到.", file);
        }
    }
}
